﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BHair.kucunpandian;
using System.Data.SqlClient;
using System.Configuration;
using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.UserDesigner;

namespace BHair
{
    public partial class frmkcpd : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        public frmkcpd()
        {
            InitializeComponent();
        }
        string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
        private void frmkcpd_Load(object sender, EventArgs e)
        {
           
            string bs = "PD-";
            string time = DateTime.Now.ToString("yyyy-MM-dd");
            Random ran = new Random();
            int RandKey = ran.Next(10, 999);
            string djh = bs + time +"-"+ RandKey.ToString();
            this.lbID.Text = djh;
            selectall();
            selectyg();
        }
        #region 查找员工
        public void selectyg()
        {
            SqlConnection conn = new SqlConnection(constr);
            try
            {
                conn.Open();
                string sql = "SELECT [eid],[ename]FROM [BHairDB].[dbo].[employee]";
                DataSet ds = new DataSet();
                SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
                sda.Fill(ds, "employee");
                cbyg.DataSource = ds.Tables["employee"];
                cbyg.DisplayMember = "ename";
            }
            catch { }
        }
        #endregion
        private void button3_Click(object sender, EventArgs e)
        {
            frmpdxz xz = new frmpdxz();
            xz.ShowDialog();
            selected();
           
           // dataGridView1.Columns[3].ReadOnly = true;
        }
        public void selected()
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql = "SELECT [spid] as 商品编号,[spname] as 商品名称,[spkc] 商品库存,[pdkc] as 盘点库存 FROM [BHairDB].[dbo].[kcpd]";
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGridView1.DataSource = dt;
            dataGridView1.Columns[0].ReadOnly = true;
            dataGridView1.Columns[1].ReadOnly = true;
            dataGridView1.Columns[2].ReadOnly = true;

        }
        #region 查询所有计算库存商品
        public void selectall()
        {
            SqlConnection conn = new SqlConnection(constr);
            try
            {
                conn.Open();
                string sql = "SELECT [iid] as 商品编号,[iname] as 商品名称,[iamount] as 当前库存 FROM [BHairDB].[dbo].[items] where jc!=2";
                SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                dataGridView2.DataSource = dt;
                conn.Close();
            }
            catch { }
        }
        #endregion
        #region 模糊查询商品
        public void likeselect()
        {
            SqlConnection conn = new SqlConnection(constr);
            try
            {
                conn.Open();
                string sql = string.Format("SELECT [iid] as 商品编号,[iname] as 商品名称,[iamount] as 当前库存 FROM [BHairDB].[dbo].[items] where jc!=2 and jianpin like '%{0}%' or iname like '%{1}%'",txtchaxun.Text,txtchaxun.Text);
                SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                dataGridView2.DataSource = dt;
            }
            catch { }
        }
        #endregion

        private void button3_Click_1(object sender, EventArgs e)
        {
            if (txtchaxun.Text.Equals(string.Empty))
            {
                selectall();
            }
            else
            {
                likeselect();
            }
        }

        private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            lbspid.Text = dataGridView2.SelectedCells[0].Value.ToString();
            this.txtname.Text = this.dataGridView2.SelectedCells[1].Value.ToString();
            this.txtdqkc.Text = this.dataGridView2.SelectedCells[2].Value.ToString();

        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (txtname.Text != string.Empty)
            {
                if (txtnum.Text != string.Empty)
                {
                    int index = this.dataGridView1.Rows.Add();
                    this.dataGridView1.Rows[index].Cells[0].Value = this.lbspid.Text;
                    this.dataGridView1.Rows[index].Cells[1].Value = this.txtname.Text;
                    //this.dataGridView2.Rows[index].Cells[2].Value = this.txtprice.Text;
                    this.dataGridView1.Rows[index].Cells[2].Value = this.txtdqkc.Text;
                    this.dataGridView1.Rows[index].Cells[3].Value = this.txtnum.Text;
                    txtname.Text = null;
                    txtnum.Text = null;
                    txtdqkc.Text = null;
                }
                else
                {
                    MessageBox.Show("请输入盘点数量");
                }
            }
            else
            {
                MessageBox.Show("请选择商品");
            }
            //this.dataGridView2.Rows[index].Cells[4].Value = decimal.Parse(this.txtnum.Text) * decimal.Parse(this.txtprice.Text);
           /* DataGridView dgv = dataGridView1;
            Total(dgv);
            */
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string yg = cbyg.Text;
            string djh = lbID.Text;
            string time = this.dateTimePicker1.Text;
            //循环插入盘点表
            try
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    string spid = this.dataGridView1.Rows[i].Cells[0].Value.ToString();
                    string spname = this.dataGridView1.Rows[i].Cells[1].Value.ToString();
                    decimal dqkc = decimal.Parse(this.dataGridView1.Rows[i].Cells[2].Value.ToString());
                    decimal pdkc = decimal.Parse(this.dataGridView1.Rows[i].Cells[3].Value.ToString());
                    string sql = string.Format("INSERT INTO [BHairDB].[dbo].[kcpd]([spid],[spname],[spkc],[pdkc],[pdyg],[pdtime],[pdID])VALUES({0},'{1}','{2}','{3}','{4}','{5}','{6}')", spid, spname, dqkc, pdkc, yg, time, djh);
                    SqlConnection conn = new SqlConnection(constr);
                    conn.Open();
                    SqlCommand comm = new SqlCommand(sql, conn);
                    comm.ExecuteNonQuery();
                    //修改商品库存
                    SqlConnection conn1 = new SqlConnection(constr);
                    conn1.Open();
                    string sql1 = string.Format("UPDATE [BHairDB].[dbo].[items]SET [iamount] = {0} WHERE iid={1}", pdkc, spid);
                    SqlCommand comm1 = new SqlCommand(sql1, conn);
                    comm1.ExecuteNonQuery();

                }
                MessageBox.Show("盘点成功");
                this.button1.Visible = false;
            }
            catch(Exception ex)
            {
                MessageBox.Show("保存出错，原因为："+ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
            DevExpress.XtraReports.UI.XtraReport xr = new DevExpress.XtraReports.UI.XtraReport();
            xr.LoadLayout(AppDomain.CurrentDomain.BaseDirectory + @"bb\kcpd.repx");
            xr.DataSource = dayin();
            xr.ShowPreview();
        }
        public DataSet dayin()
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql = string.Format("SELECT [id],[spid],[spname],[spkc],[pdkc],[pdyg],[pdtime],[pdID]FROM [BHairDB].[dbo].[kcpd] where pdID='{0}'", lbID.Text);
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            sda.Fill(ds, "kcpd");
            return ds;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            DevExpress.XtraReports.UI.XtraReport xr = new DevExpress.XtraReports.UI.XtraReport();
            xr.LoadLayout(AppDomain.CurrentDomain.BaseDirectory + @"bb\kcpd.repx");
            xr.Report.Name = "kcpd";
            xr.ShowDesignerDialog();
            xr.SaveLayout(AppDomain.CurrentDomain.BaseDirectory + @"bb\kcpd.repx");
            xr.Dispose();
        }
        /* public void Total(DataGridView dg)
{
// DataGridViewRow dgr = dg.Rows[dg.Rows.Count];
decimal sum = 0;
for (int i = 0; i < dg.Rows.Count; i++)
{
sum += Convert.ToDecimal(Convert.ToSingle(dg.Rows[i].Cells[4].Value));
}
//this.label11.Text = sum.ToString();
}
*/
    }
}
